﻿package cn.poco.util.xmlparse;

import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;
import cn.poco.bean.Restaurant;

public class RestaurantXmlparse {

	private static ArrayList<Restaurant> restaurants = null;
	private static  Restaurant restaurantBean = null;
	
	public static ArrayList<Restaurant> getXml(InputStream inSteam) throws Exception{
		
		XmlPullParser xmpparse = Xml.newPullParser();   //得到解释器
		xmpparse.setInput(inSteam, "UTF-8");           //设置解释流
		int code = xmpparse.getEventType();            //得到节点信息
		String total = null;
		String id = null;
		while(code != XmlPullParser.END_DOCUMENT){
			switch (code) {
			case XmlPullParser.START_DOCUMENT:
				//初始化
				restaurants = new ArrayList<Restaurant>();
				break;
			case XmlPullParser.START_TAG:
				if("total".equals(xmpparse.getName())){
					total = xmpparse.nextText();
				}
				if("restaurant".equals(xmpparse.getName())){
					restaurantBean = new Restaurant();
					if(total!=null && !total.equals(""))
					restaurantBean.setTotal(Integer.parseInt(total));
				}
				if(restaurantBean!=null){
					if("id".equals(xmpparse.getName())){
						id = xmpparse.nextText();
						if(!id.equals("") && id!=null){
							restaurantBean.setId(Integer.parseInt(id));
						}
					}
					if("title".equals(xmpparse.getName()) && id!=null){
						String title = xmpparse.nextText();
						restaurantBean.setTitle(title);
					}
					if("address".equals(xmpparse.getName())&& id!=null){
						String address = xmpparse.nextText();
						restaurantBean.setAddress(address);
					}
					if("vouch".equals(xmpparse.getName())&& id!=null){
						String vouch = xmpparse.nextText();
						restaurantBean.setVouch(Integer.parseInt(vouch));
					}
					if("average".equals(xmpparse.getName())&& id!=null){
						String average = xmpparse.nextText();
						restaurantBean.setAverage(average);
					}
					if("location".equals(xmpparse.getName())&& id!=null){
						String location = xmpparse.nextText();
						restaurantBean.setLocation(location);
					}
					if("area".equals(xmpparse.getName())&& id!=null){
						String area = xmpparse.nextText();
						restaurantBean.setArea(area);
					}
					if("star".equals(xmpparse.getName())&& id!=null){
						String star = xmpparse.nextText();
						restaurantBean.setStar(star);
					}
					if("dish".equals(xmpparse.getName())&& id!=null){
						String dish = xmpparse.nextText();
						restaurantBean.setDish(dish);
					}
					if("maindish".equals(xmpparse.getName())&& id!=null){
						String maindish = xmpparse.nextText();
						restaurantBean.setMaindish(maindish);
					}
					if("tel".equals(xmpparse.getName())&& id!=null){
						String tel = xmpparse.nextText();
						restaurantBean.setTel(tel);
					}
					if("intro".equals(xmpparse.getName())&& id!=null){
						String intro = xmpparse.nextText();
						restaurantBean.setIntro(intro);
					}
					if("discount".equals(xmpparse.getName())&& id!=null){
						String discount = xmpparse.nextText();
						restaurantBean.setDiscount(Float.parseFloat(discount));
					}
					if("recomment".equals(xmpparse.getName())&& id!=null){
						String recomment = xmpparse.nextText();
						restaurantBean.setRecomment(recomment);
					}
					if("comment".equals(xmpparse.getName())&& id!=null){
						String comment = xmpparse.nextText();
						restaurantBean.setComment(Integer.parseInt(comment));
					}
					if("blog-cnt".equals(xmpparse.getName())&& id!=null){
						String blog = xmpparse.nextText();
						restaurantBean.setBlog(Integer.parseInt(blog));
					}
					if("distance".equals(xmpparse.getName())&& id!=null){
						String distance = xmpparse.nextText();
						restaurantBean.setDistance(distance);
					}
					if("pepsi".equals(xmpparse.getName())&& id!=null){
						String pepsi = xmpparse.nextText();
						restaurantBean.setPepsi(Integer.parseInt(pepsi));
					}
				}	
				break;
			case XmlPullParser.END_TAG:
				if("restaurant".equals(xmpparse.getName())){
					restaurants.add(restaurantBean);
					restaurantBean = null;
				}
				break;
			}
			code = xmpparse.next();
		}
		inSteam.close();
		return restaurants;
	} 
}
